package com.briup.day27;

import com.briup.day27.bean.Student;

import java.lang.reflect.Method;

public class Test_Method {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("com.briup.day27.bean.Student");
//       1. getMethod获取所有的public方法 包含继承的
        Method[] m1 = clazz.getMethods();
        for (Method m : m1) {
            System.out.println("public修饰方法"+m);
        }
        System.out.println("----------------------");
        //不包含继承的 包含其他所有包括private
        Method[] m2 = clazz.getDeclaredMethods();
        for (Method m : m2) {
            System.out.println("所有的成员方法"+m);
        }
        System.out.println("-----------------------");
        //获取指定成员方法
        Method m3 = clazz.getMethod("getAge");
        Student s= new Student("010","larry",30);
        //等同于s.getAge
        Integer age =(Integer) m3.invoke(s);
        System.out.println(age);
        //通过调用方法给对象设置值
        Method m4 = clazz.getMethod("setAge", int.class);
        m4.invoke(s,50);
        System.out.println(s);
        System.out.println("------------");
        //操作private修饰的成员方法
        Method m5 = clazz.getDeclaredMethod("getId");
        m5.setAccessible(true);
        String s5 = (String) m5.invoke(s);
        System.out.println("id: "+s5);
        System.out.println("------------------");
        Method m6 = clazz.getDeclaredMethod("setId", String.class);
        m6.setAccessible(true);
        m6.invoke(s, "020");
        System.out.println("修改后的对象"+s);
    }
}
